<template>
  <div class="app-container">
    <el-form :model="queryParams" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学校编码">
        <el-input v-model="queryParams.schoolCode" placeholder="请输入学校编码" size="small" />
      </el-form-item>
      <el-form-item label="证书编码">
        <el-input v-model="queryParams.certificateCode" placeholder="请输入证书编码" size="small" />
      </el-form-item>
      <el-form-item label="证书类型">
        <el-select v-model="queryParams.certificateType" placeholder="请选择类型" clearable size="small">
          <el-option v-for="dict in typeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="任教学科">
        <el-input v-model="queryParams.subjectName" placeholder="请输入任教学科" size="small" />
      </el-form-item>
      <el-form-item label="毕业院校">
        <el-input v-model="queryParams.graduateSchool" placeholder="请输入毕业院校" size="small" />
      </el-form-item>
      <!-- <el-form-item label="获取时间">
        <el-input v-model="queryParams.getDate" placeholder="请输入获取时间" size="small" />
      </el-form-item>
      <el-form-item label="有效期止">
        <el-input v-model="queryParams.expirationDate" placeholder="请输入有效期止" size="small" />
      </el-form-item>
      <el-form-item label="创建人">
        <el-input v-model="queryParams.createName" placeholder="请输入创建人" size="small" />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker value-format="yyyy-MM-dd" v-model="infoValue.createTime" type="date" :editable="false"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新人">
        <el-input v-model="queryParams.updateName" placeholder="请输入更新人" size="small" />
      </el-form-item>

      <el-form-item label="更新时间">
        <el-date-picker value-format="yyyy-MM-dd" v-model="infoValue.updateTime" type="date" :editable="false"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <!-- <el-button type="warning" icon="el-icon-circle-check" size="mini" :disabled="multiple" @click="handleEnable">启用</el-button> -->
      </el-col>
      <el-col :span="1.5">
        <!-- <el-button type="warning" icon="el-icon-circle-close" size="mini" :disabled="multiple" @click="handleDisable">禁用</el-button> -->
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" icon="el-icon-upload2" size="mini" @click="handleImport">导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="teacherList" @selection-change="handleSelectionChange">
      <el-table-column label="老师姓名" align="center" prop="name" />
      <el-table-column label="学校编码" align="center" prop="schoolCode" />
      <el-table-column label="证书编码" align="center" prop="certificateCode" />
      <el-table-column label="证书类型" align="center" prop="certificateType">
        <template slot-scope="scope">
          <div>
            <span v-if="scope.row.certificateType === 1">学历证书</span>
            <span v-if="scope.row.certificateType === 2">资质证书</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="任教学科" align="center" prop="subjectName" />
      <el-table-column label="毕业院校" align="center" prop="graduateSchool" />
      <el-table-column label="获取时间" align="center" prop="getDate" width="180">

      </el-table-column>
      <el-table-column label="有效期止" align="center" prop="expirationDate" />
      <el-table-column label="创建人" align="center" prop="createName" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="更新人" align="center" prop="updateName" />
      <el-table-column label="更新时间" align="center" prop="updateTime" />
      <!-- <el-table-column label="图片" align="center"  /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改教师对话框 -->
    <el-dialog  :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body  >
      <el-form :rules="rules" :model="infoValue" ref="infoValue" :inline="true" label-width="80px">
        <el-form-item label="教师姓名" prop="teacherId">
          <el-select v-model="infoValue.teacherId" filterable placeholder="请选择教师姓名">
            <el-option v-for="item in nameList" :key="item.id" :label="item.name" :value="item.id" size="small">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学校编码" prop="schoolCode">
          <el-input v-model="infoValue.schoolCode" placeholder="请输入学校编码" size="small" />
        </el-form-item>
        <el-form-item label="证书编码" prop="certificateCode">
          <el-input v-model="infoValue.certificateCode" placeholder="请输入证书编码" size="small" />
        </el-form-item>
        <el-form-item label="证书类型" prop="certificateType">
          <el-select v-model="infoValue.certificateType" placeholder="请选择类型" clearable size="small"
            @change="certificateTypeChange">
            <el-option v-for="dict in typeOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>
        <el-form-item label="任教学科" prop="subjectName">
          <el-input v-model="infoValue.subjectName" placeholder="请输入任教学科" size="small" />
        </el-form-item>
        <el-form-item label="毕业院校" :prop="graduateSchool">
          <el-input v-model="infoValue.graduateSchool" placeholder="请输入毕业院校" size="small" />
        </el-form-item>
        <el-form-item label="获取时间" prop="getDate">
          <el-date-picker value-format="yyyy-MM-dd" v-model="infoValue.getDate" type="date" :editable="false"
            placeholder="请选择获取时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="有效期止" prop="expirationDate">
          <el-date-picker value-format="yyyy-MM-dd" v-model="infoValue.expirationDate" type="date" :editable="false"
            placeholder="请选择有效期止">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建人" prop="createName">
          <el-input v-model="infoValue.createName" placeholder="请输入创建人" size="small" />
        </el-form-item>
        <el-form-item label="上传图片">
          <span @click="getDownLink(infoValue)" v-if="infoValue.fileName != null" class="downlink">{{
            infoValue.fileName
          }}</span>
          <el-upload ref="upload" :limit="1" accept=".doc, .docx, .pdf, .jpg, .jpeg, .png" :headers="upload.headers"
            :action="upload.url" :disabled="upload.isUploading" :on-progress="handleFileUploadProgress"
            :on-success="handleFileSuccessFj">
            <el-button size="small" type="primary">点击上传</el-button>
            <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许上传文档或者图片文件！</div>
          </el-upload>
        </el-form-item>
        <!-- <el-form-item label="创建时间" prop="createTime">
            <el-date-picker value-format="yyyy-MM-dd" v-model="infoValue.createTime" type="date" :editable="false"
              placeholder="请选择创建时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="更新人" prop="updateName">
            <el-input v-model="infoValue.updateName" placeholder="请输入更新人" size="small" />
          </el-form-item>
          <el-form-item label="更新时间" prop="updateTime">
            <el-date-picker value-format="yyyy-MM-dd" v-model="infoValue.updateTime" type="date" :editable="false"
              placeholder="请选择更新时间">
            </el-date-picker>
          </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" size="mini">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { selectCerPageList, insertCerData, selectCerById, updateCerData, deleteCerById, teacherNameList } from "@/api/base/teacher";
import { getToken } from "@/utils/auth";
import { downLoadword } from "@/utils/zipdownload";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Teacher",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教师表格数据
      teacherList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 类型字典
      typeOptions: [],
      // 状态字典
      statusOptions: [],
      // 导入参数
      // 导入参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/common/upload"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        code: null,
        name: null,
        type: null,
        sex: null,
        education: null,
        degree: null,
        idcard: null,
        status: null
      },
      // 表单参数
      infoValue: {},
      // 表单校验
      rules: {
        teacherId: [
          { required: true, message: "教师姓名不能为空", trigger: "change" }
        ],
        schoolCode: [
          { required: true, message: "学校编码不能为空", trigger: "blur" }
        ],
        certificateCode: [
          { required: true, message: "证书编码不能为空", trigger: "blur" }
        ],
        certificateType: [
          { required: true, message: "证书类型不能为空", trigger: "blur" }
        ],
        subjectName: [
          { required: true, message: "任教学科不能为空", trigger: "blur" }
        ],
        graduateSchool: [
          { required: true, message: "毕业院校不能为空", trigger: "blur" }
        ],
        getDate: [
          { required: true, message: "获取时间不能为空", trigger: "blur" }
        ],
        createName: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateName: [
          { required: true, message: "更新人不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ],
        expirationDate: [
          { required: true, message: "有效期止不能为空", trigger: "blur" }
        ],
      },
      graduateSchool: '',
      nameList: [],
    };
  },
  async created() {
    await this.getTeacherList()
    await this.getList();
    this.getDicts("certificate_type").then(response => {
      this.typeOptions = response.data;
    });
  },
  methods: {
    certificateTypeChange(val) {
      if (val === '1') {
        this.graduateSchool = 'graduateSchool'
      } else {
        this.graduateSchool = ''
      }
    },
    /** 查询教师资质证书列表 */
    getList() {
      this.loading = true;
      const params = JSON.stringify(this.queryParams)
      selectCerPageList(params).then(response => {
        this.teacherList = response.rows;
        this.nameList.forEach((item, index) => {
          this.teacherList.forEach((it, ind) => {
            if (item.id === it.teacherId) {
              it.name = item.name
            }
          })
        })
        this.total = response.total;
        this.loading = false;
      }).catch(err => {
        this.loading = false;
      })
    },

    async getTeacherList() {
      const { rows, code } = await teacherNameList()
      if (code === 200) {
        this.nameList = rows
      } else {
        this.$message.error(msg)
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        code: null,
        name: null,
        type: null,
        sex: null,
        education: null,
        degree: null,
        idcard: null,
        status: null
      };
      this.infoValue = {}
      this.resetForm('infoValue');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.reset()
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加资质证书";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      selectCerById(id).then(response => {
        this.infoValue = response.data;
        this.open = true;
        this.title = "修改资质证书";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["infoValue"].validate(valid => {
        if (valid) {
          if (this.infoValue.id != null) {
            updateCerData(this.infoValue).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            insertCerData(this.infoValue).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除选中的教师数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return deleteCerById(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 启用按钮操作 */
    handleEnable(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认启用选中的教师信息？', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return enableTeacher(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 禁用按钮操作 */
    handleDisable(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认禁用选中的教师信息？', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return disableTeacher(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有教师数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportTeacher(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "教师导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      importTemplate().then(response => {
        this.download(response.msg);
      });
    },
    /** 下载附件链接 */
    getDownLink(row) {
      if (row.fileName && row.filePhoto) {
        downLoadword('/common/eos/download?fileName=' + row.fileName, row.filePhoto)
      }
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccessFj(response, file, fileList) {

      this.upload.open = false;
      this.upload.isUploading = false;
      this.infoValue.filePhoto = response.url
      this.infoValue.fileName = response.fileName
      this.$refs.upload.clearFiles();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
